﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports System.Linq
Imports ISNet.WebUI.WebTreeView
Imports System.IO
Imports Microsoft.VisualBasic.FileIO.TextFieldParser
Imports log4net
Partial Class mtJob
    Inherits System.Web.UI.Page

    Public AlertText As String

    Private Shared logger As ILog = LogManager.GetLogger("mtJob")

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        WebCombo3.EncodeOutput = True
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  รายงาน >> รายงานการตรวจสอบ Job"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If

        If IsPostBack = False Then
            Session.Remove("Search")
            Session.Remove("search_fctr")
            Session("PlanYearTxt") = Session("PlanYear")

            Dim jobEnum As New JobEnum
            For Each i In [Enum].GetNames(GetType(JobEnum.JobName))
                ddlJobName.Items.Add(i)
            Next
            ddlJobName.Items.Insert(0, "")
            For Each i In [Enum].GetNames(GetType(JobEnum.JobStatus))
                ddlJobStatus.Items.Add(i)
            Next
            ddlJobStatus.Items.Insert(0, "")
        End If
        Dim adapter As New MasterDataSetTableAdapters.FundCentersTableAdapter
        Dim data As New MasterDataSet.FundCentersDataTable
        data = adapter.GetDataByPlanYear(Val(Session("PlanYearTxt")))
        ms8.DataValueField = "FundCenterCode"
        ms8.DataTextField = "Description"
        ms8.DataSource = data
        ms8.DataBind()
        Session("SearchTxt") = ""
    End Sub
    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.JobListTableAdapter
        Dim dtTable As New MasterDataSet.JobListDataTable

        Dim sql As String = " select J.*, F.FUNDCENTERCODE + ' ' + F.DESCRIPTION  AS DESCRIPTION FROM JobList J "
        sql += " LEFT JOIN FUNDCENTERS F ON J.FUNDCENTERCODE = F.FUNDCENTERCODE AND F.PLANYEAR =  " & Val(Session("PlanYearTxt"))
        sql += " WHERE 1 = 1 "
        sql += Session("SearchTxt")
        sql += " ORDER BY JobId "
        adapter.SearchJob(dtTable, sql)
        Session("count_data") = dtTable.Rows.Count
        e.DataSource = dtTable
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type_del As String = TryCast(Request("type_del"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim Datalist() As String
            Datalist = Split(Deletet, ",")
            Dim opt As Integer
            opt = Val(Datalist(1))
            Dim adapter As New MasterDataSetTableAdapters.UsersTableAdapter
            Dim adapteruf As New MasterDataSetTableAdapters.UserFundCentersTableAdapter
            Dim l_adapter As New LoginDSTableAdapters.LoginLogTableAdapter
            If type_del = "1" Then
                If opt = 1 Then
                    Dim UserName As String
                    UserName = l_adapter.GetUserNameByID(Val(Datalist(0)))
                    adapteruf.DeleteByUserName(UserName)
                    adapter.DeleteUsers(Val(Datalist(0)))
                    logger.Info("User " & Session("users") & " delete user name " & Datalist(0))
                Else
                    Dim UserName As String
                    Try
                        UserName = l_adapter.GetUserNameByID(Val(Datalist(0)))
                    Catch ex As Exception
                        UserName = "xxx"
                    End Try
                    l_adapter.DeleteByUser(UserName)
                    logger.Info("User " & Session("users") & " delete user name " & UserName)
                End If
            ElseIf type_del = "2" Then
                If opt = 1 Then
                    For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                        Dim UserName As String
                        UserName = l_adapter.GetUserNameByID(keyValue)
                        adapteruf.DeleteByUserName(UserName)
                        adapter.DeleteUsers(keyValue)
                        logger.Info("User " & Session("users") & " delete user name " & keyValue)
                    Next keyValue
                Else
                    Dim UserName As String
                    Try
                        UserName = l_adapter.GetUserNameByID(Val(Datalist(0)))
                    Catch ex As Exception
                        UserName = "xxx"
                    End Try
                    For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                        l_adapter.DeleteByUser(keyValue)
                        logger.Info("User " & Session("users") & " delete user name " & keyValue)
                    Next keyValue

                End If
            End If
            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Session("SearchTxt") = ""
        If ddlJobName.SelectedIndex <> 0 Then
            Session("SearchTxt") += " and JobName = '" & ddlJobName.SelectedValue & "'"
        End If
        If ddlJobStatus.SelectedIndex <> 0 Then
            Session("SearchTxt") += " and JobStatus = '" & ddlJobStatus.SelectedValue & "'"
        End If
        If (txtFromVersion.Text <> " " And txtFromVersion.Text <> "") Then
            Session("SearchTxt") += " and FromVersion = '" & txtFromVersion.Text & "'"
        End If
        If (txtToVersion.Text <> " " And txtToVersion.Text <> "") Then
            Session("SearchTxt") += " and ToVersion = '" & txtToVersion.Text & "'"
        End If
        If (txtUpdateDate.Text <> " " And txtUpdateDate.Text <> "") Then 'Format dd/MM/yyyy
            Session("SearchTxt") += " and Convert(varchar(10), UpdateDate, 103) = '" & txtUpdateDate.Text & "'"
        End If
        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            If hdf1.Value.ToString <> "" Then
                Session("SearchTxt") += " AND J.FundCenterCode IN ("
                Dim Numx As Array = hdf1.Value.Split(",")
                For i = 0 To Numx.Length - 1
                    If Val(Numx(i)) <> 0 Then
                        If i <> 0 Then
                            Session("SearchTxt") += ",'"
                        Else
                            Session("SearchTxt") += "'"
                        End If
                        Session("SearchTxt") += Numx(i).ToString & "'"
                    Else
                        Session("SearchTxt") += "''"
                    End If
                Next
                Session("SearchTxt") += ") "
            End If
        Else
            Session("SearchTxt") += " AND J.FundCenterCode IN ("
            Session("SearchTxt") += GetSubFCTR(WebCombo3.Value, Val(Session("PlanYearTxt")))
            Session("SearchTxt") += ") "
        End If
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub
    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo3.InitializeDataSource
        Dim adap As New FundCenterParentTableAdapters.SelectorTableAdapter
        Dim dt As New FundCenterParent.SelectorDataTable
        Dim dt2 As New FundCenterParent.SelectorDataTable
        Dim sql = ""
        Dim prefil As String = ""
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
        sql += "ORDER BY Grouppath"
        adap.GetDataByFilter(dt2, sql)
        dt.Rows.Add("ทุกรายการ", "0", 0, "0", Val(Session("PlanYear")), "ทุกรายการ")
        dt.Merge(dt2)
        e.DataSource = dt
    End Sub
    Function GetSubFCTR(ByVal fctr_code As String, ByVal planyear As Integer) As String
        Dim gpath As String
        If fctr_code = "0" Then
            gpath = ""
        Else
            Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
            Dim Hdatatable As New FundCenterParent.FctHDataTable
            Hdatatable = Hadapter.GetDataByFctr(fctr_code, planyear)
            For Each row As FundCenterParent.FctHRow In Hdatatable
                gpath = row.grouppath
            Next
        End If
        Dim Hadapter2 As New FundCenterParentTableAdapters.FctH2TableAdapter
        Dim Hdatatable2 As New FundCenterParent.FctH2DataTable
        Dim sql As String = ""
        sql += "SELECT  Fcth.FundCenter_FundCenterCode FROM FctH , fctr_view  "
        sql += "WHERE Fcth.Fundcenter_FundcenterCode = fctr_view.Fundcentercode AND Grouppath like '" & gpath & "%'"
        sql += " AND Fcth.PlanYear = " & planyear & " And Fctr_view.PlanYear = " & planyear
        Return sql
    End Function
End Class
